from api.handler.ce.AbstractBatchCE import AbstractBatchCE
from api.model.BatchOutputPipe import BatchOutputPipe
from api.model.FdInputPipe import FdInputPipe
from munch import Munch
import json
import datetime
# from config.annotation import Value


def get_xuenian():
    now_year = int(datetime.datetime.now().year)
    return '{}-{}'.format(now_year-1, now_year)

class TeachInfoHandler(AbstractBatchCE):
    """生成老师授课基本信息"""
    def do_compute(self, output_stream: BatchOutputPipe, source_fds: FdInputPipe, params: dict):
        fd = source_fds.get_fd('VDJ_NEWJW_JSKB')       # 教务-教师课表

        sql = """select RKJSGH as code, count(RKJSGH) as couresnum, sum(JSXS) as time, sum(skbjrs) as studentnum 
FROM VDJ_NEWJW_JSKB 
WHERE xn = '{}'
GROUP BY RKJSGH
        """.format(get_xuenian())

        results = fd.query(sql)

        self.task_logger.log(len(results))

        for result in results:
            data = Munch()
            data.code = result.get('code', '')
            data.studentnum = result.get('studentnum', 0)
            data.couresnum = result.get('couresnum', 0)
            data.time = result.get('time', 0)
            output_stream.output_for_upsert(data)
